package com.sun.alen.service.Impl;

import com.sun.alen.entity.*;
import com.sun.alen.mapper.AdminMapper;
import com.sun.alen.mapper.CourseMapper;
import com.sun.alen.mapper.ProfessionMapper;
import com.sun.alen.mapper.TeacherMapper;
import com.sun.alen.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ProfessionMapper professionMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    public boolean add(Course course) {
        return courseMapper.add(course);
    }

    @Override
    public boolean delete(String id) {
        return courseMapper.delete(id);
    }
    @Override
    public boolean update(Course course) {
        return courseMapper.update(course);
    }
    @Override
    public PageBean search(String input, String select, Integer currentPage, Integer pageSize) {
        Integer start= (currentPage-1)*pageSize;
        int endIndex=0;
        List<Course> courseList=new ArrayList<>();
        long count=0;
        input="%"+input+"%";
        if(input.equals("%%")){
            courseList=courseMapper.list(start,pageSize);
            count=courseMapper.count();
        }else{
            switch (select) {
                case "1" -> {
                    courseList = courseMapper.searchByName(input, start, pageSize);
                    count = courseMapper.countByName(input);
                }
                case "2" -> {
                    List<Profession> professionList = professionMapper.searchIdByName(input);
                    for (int i = 0; i < professionList.size(); i++) {
                        List<Course> tempList = courseMapper.searchByProfession(professionList.get(i).getProfessionId());
                        courseList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, courseList.size());
                    count = courseList.size();
                    courseList = courseList.subList(start,endIndex);
                }
                case "3" ->{
                    List<Teacher> teacherList = teacherMapper.searchIdByName(input);
                    for (int i = 0; i < teacherList.size(); i++) {
                        List<Course> tempList = courseMapper.searchByTeacher(teacherList.get(i).getTeacherId());
                        courseList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, courseList.size());
                    count = courseList.size();
                    courseList = courseList.subList(start,endIndex);
                }
                default -> {
                    courseList = courseMapper.list(start, pageSize);
                    count = courseMapper.count();
                }
            }
        }
        PageBean pageBean=new PageBean(count,courseList);
        return pageBean;
    }

    @Override
    public List<Course> all() {
        return courseMapper.all();
    }

    @Override
    public PageBean searchTeacher(String input, String select, Integer teacherId, Integer currentPage, Integer pageSize) {
        Integer start = (currentPage - 1) * pageSize;
        List<Course> courseList=new ArrayList<>();
        long count=0;
        input="%"+input+"%";
        courseList = courseMapper.searchByCourseName(input,teacherId,start,pageSize);
        count = courseMapper.countByCourseName(input,teacherId);
        PageBean pageBean = new PageBean(count,courseList);
        return pageBean;
    }
}
